﻿using GalaSoft.MvvmLight.Messaging;
using KINLO.Model.MES;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace KINLO.Model.InfoModel
{
    [Serializable]
    public class InfoDataModel
    {
        private static InfoDataModel _tmp;
        public static InfoDataModel Tmp
        {
            set { _tmp = value; }
            get { return _tmp ?? (_tmp = new InfoDataModel()); }
        }
        /// <summary>
        /// 工号
        /// </summary>
        public string UserName = "";
        /// <summary>
        /// 设备编号
        /// </summary>
        public string EquipmentCode = "";
        /// <summary>
        /// 班次
        /// </summary>
        public string Class = "";
        /// <summary>
        /// 班组
        /// </summary>
        public string ClassTeam = "";
        /// <summary>
        /// 工单
        /// </summary>
        public string WorkOrder = "";
        /// <summary>
        /// 批次号
        /// </summary>
        public string BatchNo = "";
        /// <summary>
        /// 料号
        /// </summary>
        public string MaterialCode = "";
        /// <summary>
        /// 工序
        /// </summary>
        public string process_id = "";


        public string MaterialName = "";
        /// <summary>
        /// 型号
        /// </summary>
        public string MaterialSpec = "";
        /// <summary>
        /// 工单状态
        /// </summary>
        public string WorkOrderStatusName = "";
        /// <summary>
        /// 计划开始时间
        /// </summary>
        public string PlanedStart = "";
        /// <summary>
        /// 计划结束时间
        /// </summary>
        public string PlanedEndDate = "";
        /// <summary>
        /// 订单号
        /// </summary>
        public string ProductionOrderNO = "";
        /// <summary>
        /// 待机名称
        /// </summary>
        public string standbyName;
        /// <summary>
        /// 待机编码
        /// </summary>
        public int standbyNumber;

        /// <summary>
        /// 设备状态
        /// </summary>
        public string status = "0";
        /// <summary>
        /// 中单编号
        /// </summary>
        public string EmpNo = "";
        /// <summary>
        /// Mes开关
        /// </summary>
        public bool EnbleMesOnOff = false;

        /// <summary>
        /// 生产统计接口
        /// </summary>
        public string Url1 = "";
        /// <summary>
        /// 设备报修接口
        /// </summary>
        public string Url2 = "";
        /// <summary>
        /// 设备履历接口
        /// </summary>
        public string Url3 = "";
        /// <summary>
        /// 标准参数接口
        /// </summary>
        public string Url4 = "";
        /// <summary>
        /// 工单获取接口
        /// </summary>
        public string Url5 = "";
        /// <summary>
        /// 工号校验接口
        /// </summary>
        public string Url6 = "";
        /// <summary>
        /// 参数超限接口
        /// </summary>
        public string Url7 = "";
        /// <summary>
        /// 工艺参数接口
        /// </summary>
        public string Url8 = "";
        /// <summary>
        /// 生产统计上传(小时)
        /// </summary>
        public string Url9 = "";
        /// <summary>
        /// 生产统计上传(天)
        /// </summary>
        public string Url10 = "";

        /// <summary>
        /// 标准参数接口定时时间
        /// </summary>
        public string Url4Time = "";
        /// <summary>
        /// 工艺参数接口定时时间
        /// </summary>
        public string Url8Time = "";
        /// <summary>
        /// 生产统计上传(小时)定时时间
        /// </summary>
        public string Url9Time = "";
        /// <summary>
        /// 生产统计上传(天)定时时间
        /// </summary>
        public string Url10Time = "";


        //上次状态信息
        public Dictionary<int, List<MESEquipmentStatusModel>> last_StatusData = new Dictionary<int, List<MESEquipmentStatusModel>>();

        //待机原因编码
        public int last_recode { get; set; } = 0;

        public List<MESGeneralDataReturnModel> GeneralData = new List<MESGeneralDataReturnModel>();
        public bool Save(string path = "")
        {
            try
            {
                if (path.Trim() == "")
                {
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cfg.kl");
                }
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, Tmp);
                }


                return true;

            }
            catch (Exception ex)
            {
                var msg1 = string.Format($"保存失败{ex.ToString()}");
                Messenger.Default.Send(msg1, "ReceiveEventInfo");
                return false;
            }
        }
        public bool Load(string path = "")
        {
            try
            {
                if (path.Trim() == "")
                {
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cfg.kl");
                }
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    Tmp = (InfoDataModel)bf.Deserialize(fs);
                }
                return true;


            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}
